package cloud.shyidong.core.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.CannedAccessControlList;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.TransferManager;
import com.qcloud.cos.transfer.TransferManagerConfiguration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/10/13
 */
public final class CosUtil {
    
    private final static TransferManager transferManager;
    
    static {
        COSCredentials cosCredentials = new BasicCOSCredentials(SpringUtils.getProperty("cos.secret.id"), SpringUtils.getProperty("cos.secret.key"));
        Region region = new Region(SpringUtils.getProperty("cos.region.name"));
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        COSClient cosClient = new COSClient(cosCredentials, clientConfig);
        cosClient.setBucketAcl(SpringUtils.getProperty("cos.bucket.name"), CannedAccessControlList.Private);
        ExecutorService threadPool = Executors.newFixedThreadPool(32);
        transferManager = new TransferManager(cosClient, threadPool);
        TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
        transferManagerConfiguration.setMultipartUploadThreshold(5 * 1024 * 1024);
        transferManagerConfiguration.setMinimumUploadPartSize(1 * 1024 * 1024);
        transferManager.setConfiguration(transferManagerConfiguration);
    }
    
    public static TransferManager getTransferManager() {
        return transferManager;
    }
    
}
